package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.rfid.project.exception.CannotDelException;
import com.mysema.query.BooleanBuilder;

import java.util.List;

@Service
@Transactional
public class ProjectTypeService{

	@Autowired
	private ProjectTypeDao projectTypeDao;
	
	private QProjectType $ = QProjectType.projectType;

	@Autowired
	private ProjectDao projectDao;
	
	private QProject $_ = QProject.project;

	@Autowired
	private CommonMilestoneDao commonMilestoneDao;
	
	private QCommonMilestone $_1 = QCommonMilestone.commonMilestone;
	
	@Autowired
	public void setProjectTypeDao(ProjectTypeDao projectTypeDao) {
		this.projectTypeDao = projectTypeDao;
	}
	
	@Autowired
	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}
	
	@Autowired
	public void setCommonMilestoneDao(CommonMilestoneDao commonMilestoneDao) {
		this.commonMilestoneDao = commonMilestoneDao;
	}
	
	public List<ProjectType> findAll(){
		return ListUtil.toList(projectTypeDao.findAll());
	}
				
	public List<ProjectType> findAll(
	String name,
	String remark,
	String orderByAttribute	){
	BooleanBuilder condition = new BooleanBuilder();
	
	if(StringUtil.isNotEmpty(name)){
		condition.and($.name.contains(name));
	}
	
	
	if(StringUtil.isNotEmpty(remark)){
		condition.and($.remark.contains(remark));
	}
	
	
	return ListUtil.toList(projectTypeDao.findAll(condition));
	}		

	public Page<ProjectType> findAll(
			String name,
			String remark,
			int pageNumber, int pageSize,String sortAttribute,String orderDirection) {
			BooleanBuilder condition = new BooleanBuilder();
	
			if(StringUtil.isNotEmpty(name)){
				condition.and($.name.contains(name));
			}
			if(StringUtil.isNotEmpty(remark)){
				condition.and($.remark.contains(remark));
			}
	
			/*
			Sort sortObject = null;
			if(StringUtil.isEmpty(sortAttribute)){
				sortObject = new Sort(Direction.DESC, "id");
			}else{
				sortObject = new Sort(Direction.fromStringOrNull(orderDirection),sortAttribute);
			}
			*/
			
			Sort sortObject_1 = new Sort(Direction.DESC, "orderNumber");
			
			PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sortObject_1);
			return projectTypeDao.findAll(condition, pageRequest);
	}
	
	
	
	public ProjectType getProjectType(Long id){
		return projectTypeDao.getOne(id);
	}
	
	public void deleteProjectType(Long id) throws CannotDelException{
		if (findProjectByProjectType(id)) {
			throw new CannotDelException("已经有项目引用此项目类型，无法删除此项目类型！");
		} if(findCommonMilestoneByProjectType(id)){
			throw new CannotDelException("已经有项目节点引用此项目类型，无法删除此项目类型！");
		} else {
			projectTypeDao.delete(id);
		}
	}
	
	private boolean findProjectByProjectType(Long projectTypeId){
		BooleanBuilder condition = new BooleanBuilder();
		condition.and($_.projectType.id.eq(projectTypeId));
		return this.projectDao.count(condition) > 0;
	}
	
	private boolean findCommonMilestoneByProjectType(Long projectTypeId){
		BooleanBuilder condition = new BooleanBuilder();
		condition.and($_1.projectType.id.eq(projectTypeId));
		return this.commonMilestoneDao.count(condition) > 0;
	}

	public void saveProjectType(ProjectType projectType) {
		if (null == projectType.getOrderNumber()) {
			projectType.setOrderNumber(getOrderNumber());
		}
		projectTypeDao.save(projectType);
	}

	private int getOrderNumber(){
		BooleanBuilder condition = new BooleanBuilder();
		Sort sortObject = new Sort(Direction.DESC, "orderNumber");
		PageRequest pageRequest = new PageRequest(0, 1, sortObject);
		Page<ProjectType> items = this.projectTypeDao.findAll(condition,pageRequest);
		if(items.getTotalElements() > 0){
			ProjectType item = items.getContent().get(0);
			return item.getOrderNumber()+1;
		}
		return 1;
	}
	
	public boolean changeOrderNumber(Long id, Long targetId){
		ProjectType currentModule = this.projectTypeDao.findOne(id);
		ProjectType targetModule = this.projectTypeDao.findOne(targetId);
		int orderNumber = currentModule.getOrderNumber();
		currentModule.setOrderNumber(targetModule.getOrderNumber());
		targetModule.setOrderNumber(orderNumber);
		
		this.projectTypeDao.save(currentModule);
		this.projectTypeDao.save(targetModule);
		return true;
	}

	public ProjectType findOne(Long id) {
		return this.projectTypeDao.findOne(id);
	}
	
	

}
		
